﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using SYH.Public.Model;
using System.Text;
using SYH.Utils;

namespace admin.syh876.com.Controllers
{
    public class AdMgrController : Controller
    {
        //--------广告位信息管理---------//

        #region 广告位信息管理
        public ActionResult AdAreaList()
        {
            DataTable AdAreaDt = SYH.Public.BLL.Public_AdAreaInfo.GetList("").Tables[0];
            return View(AdAreaDt);
        }

        public ActionResult AdAreaDetail(int id = 0)
        {
            Public_AdAreaInfo model_AdAreaInfo = SYH.Public.BLL.Public_AdAreaInfo.GetModel(id);
            if (model_AdAreaInfo == null)
                model_AdAreaInfo = new Public_AdAreaInfo();

            return View(model_AdAreaInfo);
        }

        [HttpPost]
        public ActionResult AdAreaSave(Public_AdAreaInfo model)
        {
            Public_AdAreaInfo model_AdAreaInfo = new Public_AdAreaInfo();


            model = Common.ModelHelper.FillModel<Public_AdAreaInfo>(model);


            //过滤非法字符
            model_AdAreaInfo.Title = Common.StringManager.ClearStrictSql(model_AdAreaInfo.Title).Trim();
            if (model.Id > 0)
            {
                if (SYH.Public.BLL.Public_AdAreaInfo.Update(model_AdAreaInfo))
                {
                    return Content("1");
                }
                else
                {
                    return Content("0");
                }
            }
            else
            {
                if (SYH.Public.BLL.Public_AdAreaInfo.Add(model_AdAreaInfo) > 0)
                {
                    return Content("1");
                }
                else
                {
                    return Content("0");
                }
            }
        }

        [HttpPost]
        public ActionResult AdAreaDelete(int id = 0)
        {
            if (SYH.Public.BLL.Public_AdAreaInfo.Delete(id))
            {
                return Content("1");
            }
            else
            {
                return Content("0");
            }
        }
        #endregion

        //--------广告信息管理---------//

        #region 广告信息管理
        public ActionResult AdInfoList(string SearchKey, int AdAreaId = 0)
        {
            //广告位信息下拉框
            DataTable AdArea_Dt = SYH.Public.BLL.Public_AdAreaInfo.GetList("").Tables[0];
            ViewBag.AdArea_Dt = AdArea_Dt;

            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat(" and pi.Status=0");
            if (!string.IsNullOrEmpty(SearchKey))
            {
                strSql.AppendFormat(" and pi.Title like '%{0}%'", SearchKey);
            }
            if (AdAreaId > 0)
            {
                strSql.AppendFormat(" and pi.AdAreaId = {0}", AdAreaId);
            }

            PagerSource ps = new PagerSource("p", 10);
            int pageCount = 0;
            int recordCount = 0;
            DataTable AdInfoDt = SYH.Public.BLL.Public_AdInfo.GetPageData(ps.PageSize, ps.CustomPage, strSql.ToString(), out pageCount, out recordCount);
            return View(AdInfoDt);
        }

        public ActionResult AdInfoDetail(int id = 0)
        {
            #region  广告位信息
            DataTable AdAreaDt = SYH.Public.BLL.Public_AdAreaInfo.GetList("").Tables[0];
            ViewBag.AdArea = AdAreaDt;
            #endregion

            Public_AdInfo model_Public_AdInfo = SYH.Public.BLL.Public_AdInfo.GetModel(id);
            if (model_Public_AdInfo == null)
                model_Public_AdInfo = new Public_AdInfo();
            return View(model_Public_AdInfo);
        }

        [HttpPost]
        public ActionResult AdInfoSave(Public_AdInfo model)
        {
           
            //过滤非法字符
            model = Common.ModelHelper.FillModel<Public_AdInfo>(model);
            model.Title = Common.StringManager.ClearStrictSql(model.Title).Trim();
            if (model.Id > 0) //修改
            {
                if (SYH.Public.BLL.Public_AdInfo.Update(model))
                {
                    return Content("1");
                }
                else
                {
                    return Content("0");
                }
            }
            else//增加
            {
                if (SYH.Public.BLL.Public_AdInfo.Add(model) > 0)
                {
                    return Content("1");
                }
                else
                {
                    return Content("0");
                }
            }

        }

        [HttpPost]
        public ActionResult AdInfoDelete(int id = 0)
        {

            #region  物理删除
            if (SYH.Public.BLL.Public_AdInfo.Delete(id))
            {
                return Content("1");
            }
            else
            {
                return Content("0");
            }
            #endregion

        }

        [HttpPost]
        public ActionResult AdInfoSort(int id = 0, int value = 0)
        {
            if (id > 0)
            {
                string sqlWhere = " Id=" + id;
                if (SYH.Public.BLL.Public_AdInfo.UpdateOneField("Sort", value.ToString(), sqlWhere, false))
                {
                    return Content("1");
                }
                else
                {
                    return Content("0");
                }
            }
            else
            {
                return Content("参数有误");
            }
        }
        #endregion

        //--------友情链接信息管理---------//

        #region 友情链接信息管理
        public ActionResult FriendLinkList()
        {
            StringBuilder strSql = new StringBuilder();

            PagerSource ps = new PagerSource("p", 10);
            int pageCount = 0;
            int recordCount = 0;
            DataTable FriendLinkDt = SYH.Public.BLL.Public_FriendLink.GetPageData(ps.PageSize, ps.CustomPage, strSql.ToString(), out pageCount, out recordCount);
            return View(FriendLinkDt);
        }

        public ActionResult FriendLinkDetail(int id = 0)
        {
            Public_FriendLink model = SYH.Public.BLL.Public_FriendLink.GetModel(id);
            if (model == null)
                model = new Public_FriendLink();

            return View(model);
        }

        [HttpPost]
        public ActionResult FriendLinkSave(Public_FriendLink model)
        {
            model = Common.ModelHelper.FillModel<Public_FriendLink>(model);
            if (model.Id > 0)//修改
            {
                if (SYH.Public.BLL.Public_FriendLink.Update(model))
                {
                    return Content("1");
                }
                else
                {
                    return Content("0");
                }
            }
            else//增加
            {
                if (SYH.Public.BLL.Public_FriendLink.Add(model) > 0)
                {
                    return Content("1");
                }
                else
                {
                    return Content("0");
                }
            }
        }

        [HttpPost]
        public ActionResult FriendLinkDelete(int id = 0)
        {
            if (SYH.Public.BLL.Public_FriendLink.Delete(id))
            {
                return Content("1");
            }
            else
            {
                return Content("0");
            }
        }
        #endregion

    }
}
